package com.example.lanouhn.bantangapp.activities.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.adapters.Adapter_FTogdeillist;
import com.example.lanouhn.bantangapp.activities.adapters.Adapter_FUseroiginal;
import com.example.lanouhn.bantangapp.activities.adapters.Adapter_TogLine;
import com.example.lanouhn.bantangapp.activities.contants.MyEvent;
import com.example.lanouhn.bantangapp.activities.contants.Search_deillist_item;
import com.example.lanouhn.bantangapp.activities.contants.Search_hottagDeilist_url;
import com.example.lanouhn.bantangapp.activities.contants.Search_hottag_url;
import com.example.lanouhn.bantangapp.activities.contants.TogLine;
import com.example.lanouhn.bantangapp.activities.contants.Useroriginal;
import com.example.lanouhn.bantangapp.activities.contants.Useroriginal_Image;
import com.example.lanouhn.bantangapp.activities.utils.HttpUtils;
import com.example.lanouhn.bantangapp.activities.utils.JsonUtils_Tagdeillist;
import com.example.lanouhn.bantangapp.activities.utils.JsonUtils_TogLine;
import com.example.lanouhn.bantangapp.activities.utils.JsonUtils_Useroriginal;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/8/1.
 */
public class Fragment_Line extends Fragment {
    private RecyclerView  recyclerView;
    private List<TogLine> togdeillist;//清单实体类
    private Adapter_TogLine adapter;//adapter数据源

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_line,container,false);
        recyclerView= (RecyclerView) view.findViewById(R.id.recyclerview);
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);

        return view;
    }




    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //注册eventbus   register(this)  this 是接收的对象
        EventBus.getDefault().register(Fragment_Line.this);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }



    /**
     * 接收事件的方法
     *  @Subscribe  这个一定要写!!!!!  ThreadMode.MAIN 是在主线程  BACKGROUD是后台操作
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void initEvent(MyEvent event){
        String text = event.getText();
        initDataToPost(text);


    }

    //post解析数据
    private void initDataToPost(final String  text) {

        togdeillist = new ArrayList<>();
        new Thread(new Runnable() {
            @Override
            public void run() {


                String url = Search_hottag_url.togproductURL;//获取网络地址
                String params="app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2425199&oauth_token=f1ba94f80125dbe645e30f80634437d0&track_deviceid=868596020655823&track_device_info=m2+note&channel_name=UMENG_CHANNEL&app_installtime=1468811189&app_versions=5.8.4&os_versions=5.1&screensize=1080&v=16&keyword="+text+"&page=0&pagesize=20";

                String result = HttpUtils.doPost(url,params);

                if (null!=result&&result.length()>0){
                    togdeillist = JsonUtils_TogLine.getlineList(result);
                    handler.sendEmptyMessage(1);
                }else{
                    handler.sendEmptyMessage(0);
                }

            }


        }).start();
    }

//验证是否连接成功
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    adapter= new Adapter_TogLine(getActivity(),togdeillist);
                    recyclerView.setAdapter(adapter);
                    break;
                case 0:
                    Toast.makeText(getActivity(),R.string.fail,Toast.LENGTH_SHORT).show();
                    break;

            }
        }
    };

}
